package com.fhz.mvvmshopjava.ui.sell;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.room.util.StringUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.fhz.mvvmshopjava.R;
import com.fhz.mvvmshopjava.core.BaseActivity;
import com.fhz.mvvmshopjava.data.model.LoggedInUser;
import com.fhz.mvvmshopjava.databinding.ActivitySellBinding;
import com.fhz.mvvmshopjava.entity.Goods;
import com.fhz.mvvmshopjava.entity.Order;
import com.fhz.mvvmshopjava.pay.PayDemoActivity;
import com.fhz.mvvmshopjava.ui.login.LoginActivity;
import com.fhz.mvvmshopjava.utils.UserManager;
import com.fhz.utilsfhz.ApiResponse;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * 商品的详细信息
 */
@AndroidEntryPoint
public class SellActivity extends BaseActivity<SellViewModel,ActivitySellBinding> {

    @Inject
    UserManager userManager;

    private Goods goods;
    private Order order;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_sell;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        if(id != null || !id.equals("")){
            mViewModel.getGoods(id).observe(this,goodsApiResponse -> {
                goods = goodsApiResponse.data;
                mBinding.setGoods(goods);

                order = new Order();
                order.setState(0);
                order.setUserId(userManager.getUserId());
                order.setGoodsCount(0); //有个问题
                order.setGoodsPrice(goods.getGoodsDefaultPrice());
                order.setGoodsIcon(goods.getGoodsDefaultIcon());
                order.setGoodsDesc(goods.getGoodsDesc());
                order.setGoodsSku(goods.getGoodsDefaultSku());
                order.setGoodsId(goods.getId());
                order.setOrderId(goods.getId());

            });
        }

        //添加到购物车
        mBinding.add.setOnClickListener(v -> {
            //是否登录
            if(isLogged()){
                //添加到购物车
                //TODO 点击多次会重复添加 添加之前需要判断数据库是否已经有了.
                Toast.makeText(getApplicationContext(), "添加成功!", Toast.LENGTH_SHORT).show();
                mViewModel.addOrder(order).observe(this, new Observer<ApiResponse<Order>>() {
                    @Override
                    public void onChanged(ApiResponse<Order> orderApiResponse) {
                        //取到最添加的id

                    }
                });
                //刷新购物的fragment
            }else{
                //去登录
                LoginActivity.launch(this);
            }
        });

        //直接买
        mBinding.buy.setOnClickListener(v ->{
            //是否登录
            if(isLogged()){
                //拉起支付宝
//                startActivity(new Intent(this, PayDemoActivity.class));
                PayDemoActivity.launch(this,order);
            }else{
                //去登录
                LoginActivity.launch(this);
            }
        });
    }

    //是否登录
    private boolean isLogged() {
        LoggedInUser user = userManager.getUser();
        if(user == null){
            return false;
        }
        return true;
    }

    @Override
    protected SellViewModel createViewModel() {
        return new ViewModelProvider(this).get(SellViewModel.class);
    }

    public static void launch(Context context, String id){
        Intent intent = new Intent(context, SellActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }
}